<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Hyperf\Elasticsearch\ClientBuilderFactory;
use Psr\Container\ContainerInterface;

/**
 * @Command
 */
#[Command]
class CheckDictionaryOther extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;


    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        parent::__construct('test_dic');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }

    public function handle()
    {
        $builder = $this->container->get(ClientBuilderFactory::class)->create();
        $host = env('ELASTICSEARCH_HOST');
        $port = env('ELASTICSEARCH_PORT');
        $user = env('ELASTICSEARCH_USER');
        $pass = env('ELASTICSEARCH_PASS');
        $url = "http://{$user}:{$pass}@{$host}:{$port}";
        $client = $builder->setHosts([$url])->setSSLVerification(false)->build();

        $result = $client->search([
            'index' => 'smoothtravel-prod-member',
            'body'  => [
                'query' => [
                    'match_all' => new \stdClass()
                ],
                'size'  => 1000
            ]
        ]);
        $data = [];
        foreach ($result['hits']['hits'] as $v) {
            $data[] = $v['_source'];
        }
        $member_ids = [];
        $aItems = $bItems = [];
        $memberInfoA = $memberInfoB = [];
        foreach ($data as $item) {
            $id = $item['id'];
            foreach ($item['other_member_dictionary_id'] as $other_dictionary_id) {
                $aItems[] = $id.'-'.$other_dictionary_id;
            }
            $member_ids[$id] = 1;
            $memberInfoA[] = $id.'-'.$item['address_id'].'-'.$item['member_dictionary_id'];
            if ($id == 100300) {
                dump($id.'-'.$item['address_id'].'-'.$item['member_dictionary_id']);
            }
        }
        $member_ids = array_keys($member_ids);
        $items = Db::table("member_other_member_dictionary")->whereIn("member_id", $member_ids)->get();
        foreach ($items as $item) {
            $bItems[] = $item->member_id.'-'.$item->member_dictionary_id;
        }


        $this->line("es和db的差集：", count(array_diff($aItems, $bItems)));
        $this->line("db和es的差集：", count(array_diff($bItems, $aItems)));

        $members = Db::table("member")->whereIn("id", $member_ids)->get();
        foreach ($members as $member) {
            $memberInfoB[] = $member->id.'-'.intval($member->address_id).'-'.$member->member_dictionary_id;
        }
        $this->line("member: es和db的差集：", count(array_diff($memberInfoA, $memberInfoB)));
        $this->line("member: db和es的差集：", count(array_diff($memberInfoB, $memberInfoA)));

//        dd(array_diff($memberInfoA, $memberInfoB));
        dd(array_diff($memberInfoB, $memberInfoA));
    }
}
